let msgTimer = null;
export default {
    openPage: function(options, callback) {
        const self = this;
        let url = options;
        if (typeof options === 'object') {
            if (options.url) {
                url = options.url;
                delete options.url;
            }
        } else {
            options = {};
        }
        callback = callback || options.callback;
        uni.navigateTo({
            url: url,
            events: {
                //为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据,被打开页面的回调
                callbackFromOpenedPage: function(data) {
                    callback && callback(data);
                }
            },
            success(res) {
                if (options.params) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', options.params)
                }
            }
        })
    },
    closePage: function(options) {
        const self = this;
        uni.navigateBack(options);
    },
    showMsg: function(msg, delay, fn) {
        delay = delay || 2500;
        uni.showToast({
            mask: true,
            icon: 'none',
            title: msg,
            duration: delay,
            success: function() {
                if (typeof fn === 'function') {
                    msgTimer = setTimeout(() => {
                        // 倒计时结束，清除定时器，隐藏toast组件
                        clearTimeout(msgTimer);
                        msgTimer = null;
                        fn();
                    }, delay);
                }
            }
        });

    },
    showMsgWithClose: function(msg, delay) {
        const self = this;
        return self.showMsg(msg, delay, function() {
            self.closePage();
        });
    },
    showWait: function(msg) {
        msg = msg || '数据加载中……';
        uni.showLoading({
            title: msg,
            mask: true
        });
    },
    hideWait() {
        uni.hideLoading();
    },
    alert: function(msg, title,callback) {
        uni.showModal({
            title: title || '提示',
            showCancel: false,
            content: msg,
            success: function(res) {
                callback&&callback(res);
            }
        });
    },
    confirm: function(msg, callback, title) {
        //https://uniapp.dcloud.io/api/ui/prompt
        //hxw 2022-04-01 根据财厅要求，确定按钮要放右边
        //如果需要调整，可以通过自行控制按钮的文字，即“确定”按钮的文字其实可以设置为“取消”；
        if(this.isClient()){
            uni.showModal({
                title: title || '询问',
                content: msg,
                cancelText:'确定',
                confirmText:'取消',
                success: function(res) {
                    if (res.confirm) {
                        callback && callback(false);
                    } else if (res.cancel) {
                        callback && callback(true);
                    }
                }
            });
        }else{
            uni.showModal({
                title: title || '询问',
                content: msg,
                success: function(res) {
                    if (res.confirm) {
                        callback && callback(true);
                    } else if (res.cancel) {
                        callback && callback(false);
                    }
                }
            });
        }
    },
}
